package com.bot.backend.service.impl.user.snake;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bot.backend.mapper.SnakeMapper;
import com.bot.backend.pojo.Snake;
import com.bot.backend.pojo.User;
import com.bot.backend.service.impl.utils.UserDetailsImpl;
import com.bot.backend.service.user.snake.GetSnakeListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Queue;

@Service
public class GetSnakeListServiceImpl implements GetSnakeListService {
    @Autowired
    private SnakeMapper snakeMapper;

    @Override
    public List<Snake> getSnakeList() {
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = (UsernamePasswordAuthenticationToken)
                SecurityContextHolder.getContext().getAuthentication();

        UserDetailsImpl loginUser = (UserDetailsImpl) usernamePasswordAuthenticationToken.getPrincipal();

        User user = loginUser.getUser();

        QueryWrapper<Snake> snakeQueryWrapper = new QueryWrapper<>();
        snakeQueryWrapper.eq("user_id", user.getId());
        return snakeMapper.selectList(snakeQueryWrapper);
    }
}
